package com.gxa.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.dto.CommunityDto;
import com.gxa.dto.ResponseResult;
import com.gxa.entity.Community;
import com.gxa.service.CommunityService;
import com.gxa.service.impl.CommunityServiceImpl;
import com.gxa.vo.CommunityVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/community/*")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI(); // 获取uri
        String path = uri.substring(uri.lastIndexOf("/")); // 截取数据接口

        CommunityService communityService = new CommunityServiceImpl();

        if ("/queryAllCommunity".equals(path)){
            doQqueryAllCommunity(req,resp,communityService);
        } if ("/queryAllCommunityByPageAndComDto".equals(path)){
            doQqueryAllCommunityByPageAndComDto(req,resp,communityService);
        }
    }

    /**
     * （带参数）分页查询所有的小区
     * @param req
     * @param resp
     * @param communityService
     */
    private void doQqueryAllCommunityByPageAndComDto(HttpServletRequest req, HttpServletResponse resp, CommunityService communityService) {
        // 执行communityService
        List<CommunityVo> communities = null;
        int count = 0;

            /*// 1、获取被封装为CommunityDto对象的参数
            BufferedReader reader = req.getReader(); // 获取BufferedReader对象
            String readLine = reader.readLine(); // 读取参数
            // 利用fastJson进行参数获取直接转对象
            CommunityDto communityDto = JSON.parseObject(readLine, CommunityDto.class);*/

            String page = req.getParameter("page");
            String limit = req.getParameter("limit");

            //接收带条件的参数
            String searchParams = req.getParameter("searchParams");
            System.out.println(searchParams);
            CommunityDto communityDto = JSON.parseObject(searchParams, CommunityDto.class);
            System.out.println(communityDto);

            // 分页查询
            communities = communityService.queryAllCommunityByPageAndComDto(page,limit,communityDto);
            // 数据表总记录数
        try {
            count = communityService.count(communityDto);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // 转换结果为layui能够解析的json发送到客户端
        ResponseResult result = new ResponseResult(0, "success", count, communities);

        // 复用自定义的writeJson方法
        String toJSONString = JSON.toJSONString(result);
        writeJson(toJSONString, resp);
    }

    /**
     * 查询所有的小区
     * @param req
     * @param resp
     */
    private void doQqueryAllCommunity(HttpServletRequest req, HttpServletResponse resp, CommunityService communityService) {
        // 执行communityService
        List<Community> communities = null;
        try {
            communities = communityService.queryAllCommunity();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
        }

        // 转换结果为layui能够解析的json发送到客户端
        ResponseResult result = new ResponseResult(0, "success", 0, communities);

        // 复用自定义的writeJson方法
        String toJSONString = JSON.toJSONString(result);
        writeJson(toJSONString, resp);
    }


    /**
     * 向客户端输出转化为json的数据流
     *
     * @param json_str json字符串
     * @param resp HttpServletResponse对象
     */
    public void writeJson(String json_str, HttpServletResponse resp) {

        // 设置输出的数据格式与
        resp.setContentType("text/json;charset=UTF-8");
        // 获取输出流对象
        PrintWriter out = null;
        try {
            out = resp.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        out.println(json_str);
    }
}
